package cs2340.droidceptiontodo.test;

import java.util.ArrayList;
import java.util.List;
import cs2340.droidceptiontodo.Database;
import cs2340.droidceptiontodo.ToDoItem;
import android.test.AndroidTestCase;
import android.test.RenamingDelegatingContext;

public class DatabaseTesting extends AndroidTestCase {
	private Database db;
	private List<ToDoItem> items;
	private static final String TEST_FILE_PREFIX = "test_";
	
	@Override
	protected void setUp() throws Exception {
        super.setUp();
        
        RenamingDelegatingContext context = new RenamingDelegatingContext(getContext(), TEST_FILE_PREFIX);
        
        items = new ArrayList<ToDoItem>();
        db = new Database(context);
        db.open();
        
        items.add(new ToDoItem("0", "user", "details", "Work", "2015/04/05", "12:05", "location", true, null));
		items.add(new ToDoItem("1", "user", "details", "Work", "2014/04/05", "12:05", "location", false, null));
		items.add(new ToDoItem("2", "user", "details", "School", "2012/01/05", "12:05", "location", true, null));
		items.add(new ToDoItem("3", "user", "details", "School", "2012/12/05", "12:05", "location", false, null));
		items.add(new ToDoItem("4", "user", "details", "Personal", "2012/12/01", "12:05", "location", false, null));
		items.add(new ToDoItem("5", "user", "details", "Personal", "2011/04/05", "12:05", "location", true, null));
		db.open();
		for(ToDoItem item:items) {
			db.addItem(item);
		}
	}
	
	public void testPreConditions() {
	    assertNotNull(db);
	}
	
	public void testGetFilteredItems() {
		List<ToDoItem> expected = new ArrayList<ToDoItem>();
		List<ToDoItem> actual;
		
		// Test filtering by category
		expected.add(items.get(0));
		expected.add(items.get(1));
		actual = db.getFilteredItems("user", "Work", null, null, null);
		assertEquals(expected.size(), actual.size());
		for(int i=0; i<expected.size(); i++) {
			assertEquals(expected.get(i).getName(), actual.get(i).getName());
		}
		expected = new ArrayList<ToDoItem>();
		expected.add(items.get(2));
		expected.add(items.get(3));
		actual = db.getFilteredItems("user", "School", null, null, null);
		assertEquals(expected.size(), actual.size());
		for(int i=0; i<expected.size(); i++) {
			assertEquals(expected.get(i).getName(), actual.get(i).getName());
		}
		expected = new ArrayList<ToDoItem>();
		expected.add(items.get(4));
		expected.add(items.get(5));
		actual = db.getFilteredItems("user", "Personal", null, null, null);
		assertEquals(expected.size(), actual.size());
		for(int i=0; i<expected.size(); i++) {
			assertEquals(expected.get(i).getName(), actual.get(i).getName());
		}
		
		// Test filtering by Date
		expected = new ArrayList<ToDoItem>();
		expected.add(items.get(2));
		expected.add(items.get(4));
		expected.add(items.get(3));
		expected.add(items.get(1));
		expected.add(items.get(0));
		actual = db.getFilteredItems("user", null, null, "duedate", "2012/01/05");
		assertEquals(expected.size(), actual.size());
		for(int i=0; i<expected.size(); i++) {
			assertEquals(expected.get(i).getName(), actual.get(i).getName());
		}
		expected = new ArrayList<ToDoItem>();
		expected.add(items.get(3));
		expected.add(items.get(1));
		expected.add(items.get(0));
		actual = db.getFilteredItems("user", null, null, "duedate", "2012/12/05");
		assertEquals(expected.size(), actual.size());
		for(int i=0; i<expected.size(); i++) {
			assertEquals(expected.get(i).getName(), actual.get(i).getName());
		}
		
		// Test filtering by isChecked
		expected = new ArrayList<ToDoItem>();
		expected.add(items.get(0));
		expected.add(items.get(2));
		expected.add(items.get(5));
		actual = db.getFilteredItems("user", null, "true", null, null);
		assertEquals(expected.size(), actual.size());
		for(int i=0; i<expected.size(); i++) {
			assertEquals(expected.get(i).getName(), actual.get(i).getName());
		}
	}
	
	public void testSetSetting() {
		
		// Test adding a setting
		db.setSetting("testName", "testValue");
		assertEquals("testValue", db.getSetting("testName"));
		
		// Test updating a setting
		db.setSetting("testName", "testValue2");
		assertEquals("testValue2", db.getSetting("testName"));
	}
	
	@Override
	protected void tearDown() throws Exception {
	    super.tearDown();

	    db.close();
	    db = null;
	}
}
